package com.yxhl.zoume.core.func.appraisal.presenter;

import android.content.Context;
import android.support.annotation.NonNull;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.func.appraisal.view.AppraisalView;
import com.yxhl.zoume.data.http.rest.param.appraisal.AppraisalParam;
import com.yxhl.zoume.data.http.rest.response.appraisal.AppraisalResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.appraisal.AppraisalUseCase;
import com.yxhl.zoume.utils.CollectionUtils;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by vhawk on 16/9/18.
 */

public class AppraisalPresenter extends BasePresenter {

    @Inject
    AppraisalUseCase useCase;
    private AppraisalView view;

    @Inject
    public AppraisalPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        this.view = (AppraisalView) view;
    }

    public void appraisal(String driverId, int judgePoint, List<String> labelList, String desc) {
        StringBuffer labelIds = getLabelIds(labelList);
        AppraisalParam param = new AppraisalParam();
        param.setDesc(desc)
                .setPoint(judgePoint)
                .setLabelId(labelIds.toString())
                .setId(driverId)
                .setToken(getToken())
                .setMobile(getMobile());
        useCase.execute(param,new ZMSubscriber<AppraisalResponse>() {
                    @Override
                    public void onNext(AppraisalResponse response) {
                        super.onNext(response);
                        if (response.isSucc()) {
                            view.handleAppraisalSucceed();
                        } else {
                            view.handleAppraisalFailure(response.getResultMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.handleAppraisalError();
                    }
                });
    }

    /**
     * 拼装标签的id
     *
     * @param labelList
     * @return
     */
    @NonNull
    private StringBuffer getLabelIds(List<String> labelList) {
        StringBuffer labelIds = new StringBuffer();
        if (!CollectionUtils.isEmpty(labelList)) {
            for (String label : labelList) {
                labelIds.append(label);
                labelIds.append(",");
            }
            labelIds.delete(labelIds.length() - 1, labelIds.length());
        }
        return labelIds;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        useCase.unsubscribe();
    }
}
